#倒入相应的模块
import pygame
from pygame.locals import *
import time,random,math
from math import sqrt

class HeroPlane:
	'''玩家飞机'''
	def __init__(self,screen_temp):
		self.x = 200
		self.y = 400
		self.screen = screen_temp
		self.image = pygame.image.load('./images/me.png')
		self.bullet_list = [] #玩家子弹列表

	def display(self):
		'''绘制飞机子弹'''
		for b in self.bullet_list:
			b.display()
			if b.move():
				self.bullet_list.remove(b)

		'''绘制玩家飞机'''
		self.screen.blit(self.image,(self.x,self.y))

	def move_left(self):
		'''左移动飞机'''
		self.x -= 5
		if self.x <= 0:
			self.x = 0

	def move_right(self):
		'''右移动飞机'''
		self.x += 5
		if self.x >= 406:
			self.x = 406

	def move_up(self):
		'''上移动飞机'''
		self.y -= 5
		if self.y <= 0:
			self.y = 0

	def move_down(self):
		'''下移动飞机'''
		self.y += 5
		if self.y >= 492:
			self.y = 492

	def fire(self):
		self.bullet_list.append(Bullet(self.screen,self.x,self.y))

class Bullet:
	'''子弹类'''
	def __init__(self,screen_temp,x,y,enemy = False):
		self.x = x + 53
		self.y = y
		self.screen = screen_temp
		self.image = pygame.image.load('./images/pd.png')
		self.enemy = enemy

	def display(self):
		'''绘制子弹'''
		self.screen.blit(self.image,(self.x,self.y))

	def move(self):
		'''子弹的移动'''
		if self.enemy:
			self.y += 10
			if self.y >= 570:
				return True
		else:
			self.y -= 10
			if self.y <= -20:
				return True

class Boom:
	'''爆炸类'''
	def __init__(self,screen_temp,x,y):
		self.x = x + 53
		self.y = y
		self.screen = screen_temp
		self.image = pygame.image.load('./images/test3/bomb.png')

	def display(self):
		'''绘制爆炸'''
		self.screen.blit(self.image,(self.x,self.y))


class EnemyPlane:
	'''敌人类'''
	def __init__(self,screen_temp):
		self.x = random.choice(range(408))
		self.y = -75
		self.screen = screen_temp
		self.image = pygame.image.load('./images/e'+str(random.choice(range(3)))+'.png')
		self.bullet_list = []

	def display(self):
		
		#敌人发射子弹判断
		if random.choice(range(50)) == 10:
			self.bullet_list.append(Bullet(self.screen,self.x,self.y,enemy = True))

		#绘制敌人子弹
		for bo in self.bullet_list:
			bo.display()
			if bo.move():
				self.bullet_list.remove(bo)

		'''绘制敌人'''
		self.screen.blit(self.image,(self.x,self.y))

	def move(self,hero):
		'''敌人的移动'''
		self.y += 4

		if self.y > 568:
			return True

		#遍历所有子弹,实现子弹与敌人碰撞检测
		for bo in hero.bullet_list:
			if bo.x > self.x + 12 and bo.x < self.x + 92 and bo.y > self.y + 20 and bo.y < self.y +60:
				hero.bullet_list.remove(bo)
				return True

		#碰撞检测
		distance = sqrt(float(hero.x - self.x)**2 + float(hero.y - self.y)**2)
		if distance < 75:
			return True

#键盘控制函数
def key_control(hero_temp):
	#执行退出操作
	for event in pygame.event.get():
		if event.type == QUIT:
			print('exit()')
			exit()

	#获取按键信息
	pressed_keys = pygame.key.get_pressed()
	if pressed_keys[K_LEFT] or pressed_keys[K_a]:
		print('left...')
		hero_temp.move_left()

	elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
		print('right...')
		hero_temp.move_right()

	if pressed_keys[K_UP] or pressed_keys[K_w]:
		print('left...')
		hero_temp.move_up()

	elif pressed_keys[K_DOWN] or pressed_keys[K_s]:
		print('right...')
		hero_temp.move_down()

	if pressed_keys[K_SPACE]:
		print('space...')
		hero_temp.fire()
	#做判断,执行操作

#主程序函数
def main():

	#创建游戏窗口
	screen = pygame.display.set_mode((512,568),0,0)

	#创建游戏背景
	background = pygame.image.load('./images/bg2.jpg')

	#创建玩家飞机
	hero = HeroPlane(screen)

	m = -968
	enemylist = [] #存放敌人的列表

	while True:
		#绘制图片
		screen.blit(background,(0,m))
		m += 2
		if m >= -200:
			m = -968
		#绘制玩家飞机
		hero.display()

		#执行键盘控制
		key_control(hero)

		#随机绘制敌人
		if random.choice(range(50)) == 10:
			enemylist.append(EnemyPlane(screen))

		#遍历敌人 绘制移动
		for em in enemylist:
			em.display()
			if em.move(hero):
				enemylist.remove(em)
				Boom.display(em)
				print('boom...')

		#更新显示
		pygame.display.update()

		#定时显示
		time.sleep(0.04)

#判断当前是否为主运行成绩
if __name__ == '__main__':
	main()
